package main

import (
	"encoding/json"

	"gitee.com/youkelike/ziam/zapp/app"
	"gitee.com/youkelike/ziam/zoptions"

	"gitee.com/youkelike/ziam/zoptions/options"
)

var _ app.CliOptions = &Options{}

type Options struct {
	ServerRunOptions *options.ServerRunOptions `json:"server" mapstructure:"server"`
	MySQLOptions     *options.MySQLOptions     `json:"mysql" mapstructure:"mysql"`
	RedisOptions     *options.RedisOptions     `json:"redis" mapstructure:"redis"`
}

func NewOptions() *Options {
	return &Options{
		ServerRunOptions: options.NewServerRunOptions(),
		MySQLOptions:     options.NewMySQLOptions(),
		RedisOptions:     options.NewRedisOptions(),
	}
}

func (o *Options) Flags() (nfs zoptions.NamedFlagSets) {
	o.ServerRunOptions.AddFlags(nfs.FlagSet("server"))
	o.MySQLOptions.AddFlags(nfs.FlagSet("mysql"))
	o.RedisOptions.AddFlags(nfs.FlagSet("redis"))
	return nfs
}

func (o *Options) String() string {
	data, _ := json.Marshal(o)
	return string(data)
}

func (o *Options) Complete() error {
	return nil
}

func (o *Options) Validate() []error {
	var errs []error
	errs = append(errs, o.ServerRunOptions.Validate()...)
	errs = append(errs, o.MySQLOptions.Validate()...)
	errs = append(errs, o.RedisOptions.Validate()...)
	return errs
}
